from CvPythonExtensions import *

from PlanetfallSpells import PlanetfallSpellBuildingTargetBase
import PlanetfallSpellsUtils as PSUtils

gc = CyGlobalContext()

DESTROY_BUILDING_CAST_RANGE = 2

class DestroyBuilding(PlanetfallSpellBuildingTargetBase):
    def __init__(self):
        PlanetfallSpellBuildingTargetBase.__init__(self)

        self.pCastingUnits = []
        # Fill list with units that can cast, such as..
        #self.pCastingUnits.append( gc.getInfoTypeForString("UNIT_NAME_1") )
        #self.pCastingUnits.append( gc.getInfoTypeForString("UNIT_NAME_2") )

    def canUnitCast(self, pCastingUnit, pCastingPlot, bVisible):
        if not pCastingUnit.getUnitType() in self.pCastingUnits:
            return False
        
        return True

    def getRangePlots(self, pCastingGroup, pCastingPlot, pHoverPlot):
        pRangePlots = PSUtils.getRangePlotDistanceList(pCastingPlot, DESTROY_BUILDING_CAST_RANGE)
        return pRangePlots
        

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pPotentialPlot):
        if not PSUtils.isPlotDistanceInRange(pCastingPlot, DESTROY_BUILDING_CAST_RANGE, pPotentialPlot):
            return False

        if not pPotentialPlot.isVisible(pCastingGroup.getTeam(), False):
            return False

        return True


    def startSpellResultsOnBuilding(self, pCastingGroup, pCity, iBuilding):
        MESSAGE_LENGTH = 10
        MESSAGE = "Your %s in %s has been destroyed by %s" % (gc.getBuildingInfo(iBuilding).getDescription(), pCity.getName(), gc.getPlayer(pCastingGroup.getOwner()).getName() )
        CyInterface().addMessage(pCity.getOwner(), True, MESSAGE_LENGTH, MESSAGE, None, InterfaceMessageTypes.MESSAGE_TYPE_MINOR_EVENT,
                                 gc.getBuildingInfo(iBuilding).getButton(), ColorTypes.NO_COLOR, 0, 0, True, True)

        CyEngine().triggerEffect( 40, pCity.plot().getPoint() )
        pCity.setHasRealBuilding(iBuilding, False)

        
